home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus Special 23
/
AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso
/
Updates
/
Hardware
/
BlizKick
/
Modules
/
PatchMath020.ASM
< prev
next >
Wrap
Assembly Source File
|
1999-11-06
|
17KB
|
813 lines
; FILE: Source:modules/PatchMath020.ASM REV: 1 --- Patches C-compiler math routines with 020+ code
;
; PatchMath020 module for BlizKick
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; This module replaces C-compiler math routines with 020+
; opcodes. V1.1 enables non-64bit mul/div patches for 060.
; Also sped-up patch scanning a lot.
;
; Written by Harry Sintonen.
; This source code is Public Domain.
;
; NOTE: Optimization routine was sent to me by Dave Jones.
; Thanks a lot Dave!
;
;
incdir "include:"
include "exec/execbase.i"
include "exec/libraries.i" ; Some required...
include "exec/exec_lib.i"
include "blizkickmodule.i"
SECTION PATCH,CODE
_DUMMY_LABEL
BK_PTC
; Code is run with following incoming parameters:
;
; a0=ptr to ROM start (buffer) eg. $1DE087B8
; a1=ptr to ROM start (ROM) eg. $00F80000 (do *not* access!)
; d0=ROM lenght in bytes eg. $00080000
; a2=ptr to _FindResident routine (will search ROM buffer for resident tag):
; CALL: jsr (a2)
; IN: a0=ptr to ROM, d0=rom len, a1=ptr to resident name
; OUT: d0=ptr to resident (buf) or NULL
; a3=ptr to _InstallModule routine (can be used to plant a "module"):
; CALL: jsr (a3)
; IN: a0=ptr to ROM, d0=rom len, a1=ptr to module, d6=dosbase
; OUT: d0=success
; a4=ptr to _Printf routine (will dump some silly things (errormsg?) to stdout ;-)
; CALL: jsr (a4)
; IN: a0=FmtString, a1=Array (may be 0), d6=dosbase
; OUT: -
; d6=dosbase, a6=execbase
;
; Code should return:
;
; d0=true if succeeded, false if failed.
; d1-d7/a0-a6 can be trashed. a7 *must* be preserved! ;-)
LTSFVARSTACK EQU 8
STRUCTURE KTSTR,0
APTR KT_LoadAddr ; scan start
ULONG KT_LoadFileSize ; scan lenght
APTR KT_CallBack ; null or ptr to printf func
STRUCT KT_PrintFVarStack,LTSFVARSTACK
LABEL KTSTR_SIZEOF
Push MACRO
move.l \1,-(sp)
ENDM
Pop MACRO
move.l (sp)+,\1
ENDM
Pushm MACRO
movem.l \1,-(sp)
ENDM
Popm MACRO
movem.l (sp)+,\1
ENDM
Call MACRO
jsr (_LVO\1,a6)
ENDM
btst #AFB_68020,(AttnFlags+1,a6) ; need minimum 020
bne.b .cont
.fail moveq #0,d0
rts
.cont
tst.b (AttnFlags+1,a6) ; If no 060 enable all patches:
bpl.b .noenall
lea (C_Opt1,pc),a5
.enableall tst.l (a5)
beq.b .noenall
st (a5) ; opt_enable
lea (OptimStructSize,a5),a5
bra.b .enableall
.noenall
move.l a4,_printf ; Init callback hook
move.l d6,_d6reg
lea (ktstr,pc),a4 ; Build KTSTR structure
move.l a0,(KT_LoadAddr,a4)
move.l d0,(KT_LoadFileSize,a4)
lea (_callback,pc),a0
move.l a0,(KT_CallBack,a4)
; Patch_CompilerRoutines
; IN: a1=KTSTR
; OUT: -
move.l a4,a1 ; ...and go
bsr Patch_CompilerRoutines
moveq #1,d0 ; Set to 0 for debug
rts
; IN: a0=string to print (no linefeed!), a4=KTSTR
; OUT: -
;NOTE: may trash a0-a4/a6 and d0-d1 (at least)
_printf dc.l 0
_d6reg dc.l 0
_callback move.l d6,-(sp)
move.l (_d6reg,pc),d6 ; get orig d6 (DosBase)
lea (KT_PrintFVarStack,a4),a1
move.l (_printf,pc),a2 ; get printf
jsr (a2) ; print it
lea (.lf,pc),a0 ; print linefeed
jsr (a2)
move.l (sp)+,d6
rts
.lf dc.b 10,0
CNOP 0,2
ktstr ds.b KTSTR_SIZEOF
OPT O- ; Turn off all optimizations
* Dave Jones:
*
* " Finally, here's a routine from KickTool (Now inactive) that you
* may like to add to BlizKick. It replaces C compiler Math
* routines with 68020 opcodes. It probably will need a little
* hacking to get it to work with BlizKick, but I'm sure your more
* than capable of doing this..
*
* Note also, that this is a generic routine I wrote for all-purpose
* exe files, but not all these compiler routines are in kickstarts,
* so you may remove some of them. I guess you've got quite a few
* kicks that you can test on, to decide which ones need to remain. "
*#############################################################################
*#######
*####### ROUTINES BY DAVE JONES, MODIFIED BY HS
*#######
*####### Note that it would be nice to ask Dave Jones for the permission and
*####### the original routines if you intend to use these. This mostly because
*####### I can't be sure if I haven't messed up things again... ;)
*#######
*#############################################################################
*HS:
* Made this thing run faster.
STRUCTURE optim,0
UWORD opt_enable
UWORD opt_offset
UWORD opt_size
UWORD opt_repsize
UWORD opt_repoffset
UWORD opt_txtoffset
LABEL OptimStructSize
Patch_CompilerRoutines
Pushm d0-d2/a0-a5
Move.l a1,a4
Move.l KT_LoadAddr(a4),a0
move.l KT_LoadFileSize(a4),d2
add.l a0,d2
;******************************************************************
lea C_Opt1(PC),a2
.SpotOptimisation
tst.w (a2) ;opt_enable
beq.b .not_enabled
move.w opt_offset(a2),a1 ; Get ptr to original C to find
add.l a2,a1
.HuntLoop
move.l a1,a5
; Find
move.w opt_size(a2),d1 ; size of original C
move.l a0,a3
subq #1,d1 ; Correct for the dbcc
.seek cmpm.w (a5)+,(a3)+
dbne d1,.seek
bne.b .NotFound
bsr.b .FoundOptim
.NotFound addq.l #2,a0
cmp.l a0,d2
bgt.b .HuntLoop ; Go back
;******************************************************************
; If we get here, then we've scanned to EOF for the current
; optimisation, so move back to beginning of file, and
; try a different optimisation.
Move.l KT_LoadAddr(a4),a0 ; restore a0
.not_enabled Lea OptimStructSize(a2),a2
Tst.l (a2) ; get next optimisation
Bne.b .SpotOptimisation
.NoMorePatches Popm d0-d2/a0-a5
Rts
;-------------------------------------------------------------------
CNOP 0,4
.FoundOptim Tst.l KT_CallBack(a4)
Beq.b .QuietMode
Moveq.l #0,d1
Move.w opt_txtoffset(a2),d1
Add.l a2,d1
Move.l d1,KT_PrintFVarStack(a4) ; Name
Pushm a0-a4/a6
Sub.l KT_LoadAddr(a4),a0
Move.l a0,KT_PrintFVarStack+4(a4)
Lea FoundTxt(pc),a0 ; body
Move.l KT_CallBack(a4),a2
Jsr (a2)
Popm a0-a4/a6
.QuietMode
Move.w opt_repsize(a2),d0 ; size of my version
Subq.l #1,d0 ; correct for dbf
Move.w opt_repoffset(a2),a1 ; addr of rep code
Add.l a2,a1
.CopyPatch Move.w (a1)+,(a0)+
dbf d0,.CopyPatch
Move.w opt_size(a2),d0 ; original C size in words
Sub.w opt_repsize(a2),d0 ; Mycode size in words
Subq.l #1,d0
.ClrLoop Clr.w (a0)+
dbf d0,.ClrLoop
Rts
FoundTxt Dc.b "Patched %s routine at offset $%lx",0
CNOP 0,2
;-------------------------------------------------------------------
*HS:
* Added parameter \2 that tells whether this patch should be done on
* 060.
Optim Macro
.List\1 Dc.w \2
Dc.w \1-.List\1
Dc.w \1Size/2
Dc.w \1RepSize/2
Dc.w \1Rep-.List\1
Dc.w \1Txt-.List\1
\1DEF SET 1
Endm
*HS:
* Here you can enable different optimizations. Only DiceC Mulu and Divs
* can be found from current ROM images.
*
C_Opt1 Optim DiceC_Mulu,1
Optim DiceC_Divs,0
;Optim DiceC_MovMem,1
;Optim DiceC_StackLongMuls,1
;Optim Manx_Divs,1
;Optim Manx_Mulu,1
;Optim Manx_Mods,0
;Optim Manx_DivuModu,0
;Optim Manx_DivuModu2,0
;Optim SASMuls,1
;Optim SASMulu,1
;Optim SASDivsMods,0
;Optim SASDivuModu,0
;Optim SASDivsL,0
;Optim GenericMul1,0
dc.l 0 ** END MARKER!
***************************************************
IFD SASMulsDEF
SASMuls
__H0_end movem.l D1-D4,-(SP)
move.l D0,D4
eor.l D1,D4
tst.l D0
beq.b lbC001C9C
bpl.b lbC001C70
neg.l D0
lbC001C70 move.l D0,D2
tst.l D1
bne.b lbC001C7A
clr.l D0
bra.b lbC001C9C
lbC001C7A bpl.b lbC001C7E
neg.l D1
lbC001C7E move.l D0,D3
mulu D1,D3
swap D2
mulu D1,D2
swap D2
clr.w D2
add.l D2,D3
swap D1
mulu D1,D0
swap D0
clr.w D0
add.l D3,D0
tst.l D4
bpl.b lbC001C9C
neg.l D0
lbC001C9C movem.l (SP)+,D1-D4
rts
SASMulsSize = *-SASMuls
;==================================================
SASMulsRep
_rep0 muls.l D1,D0
rts
SASMulsRepSize = *-SASMulsRep
SASMulsTxt Dc.b "SAS C Muls",0
CNOP 0,2
ENDC
***************************************************
IFD SASMuluDEF
SASMulu
_seq1 movem.l D1-D3,-(SP)
move.l D0,D2
beq.b lbC001CD0
tst.l D1
bne.b lbC001CB8
clr.l D0
bra.b lbC001CD0
lbC001CB8 move.l D0,D3
mulu D1,D3
swap D2
mulu D1,D2
swap D2
clr.w D2
add.l D2,D3
swap D1
mulu D1,D0
swap D0
clr.w D0
add.l D3,D0
lbC001CD0 movem.l (SP)+,D1-D3
rts
SASMuluSize = *-SASMulu
;==================================================
SASMuluRep
_rep1 mulu.l D1,D0
rts
SASMuluRepSize